package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.SettleItemEntity;
import cn.mumzone.pos.ui.widgets.DashView;

/**
 * Created by qiuzhenhuan on 2019/1/8.
 */

public class ADA_BillSettleList extends RecyclerView.Adapter<ADA_BillSettleList.ViewHolder> {

    private Context mContext;

    private List<SettleItemEntity> mData;

    public ADA_BillSettleList(Context context, List<SettleItemEntity> data) {
        this.mContext = context;
        this.mData = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == SettleItemEntity.TYPE_TITLE) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_bill_settle_list_title, parent, false);
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_bill_settle_list_item, parent, false);
        }
        return new ViewHolder(view, viewType);
    }

    public void update(List<SettleItemEntity> list) {
        mData.clear();
        mData.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        int viewType = getItemViewType(position);
        SettleItemEntity item = mData.get(position);
        if (viewType == SettleItemEntity.TYPE_TITLE) {
            holder.tvTitleName.setText(item.getName());
            holder.tvTitleValue.setText(StringUtils.get(item.getValue()));

        } else {
            holder.tvItemName.setText(item.getName());
            holder.tvItemValue.setText(StringUtils.get(item.getValue()));
        }

    }


    @Override
    public int getItemViewType(int position) {
        return mData.get(position).getType();
    }

    @Override
    public int getItemCount() {
        return ListUtil.isListEmpty(mData) ? 0 : mData.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitleName;
        TextView tvTitleValue;
        TextView tvItemName;
        TextView tvItemValue;

        public ViewHolder(View itemView, int viewType) {
            super(itemView);
            if (viewType == SettleItemEntity.TYPE_TITLE) {
                tvTitleName = itemView.findViewById(R.id.tv_title_name);
                tvTitleValue = itemView.findViewById(R.id.tv_title_value);
            } else {
                tvItemName = itemView.findViewById(R.id.tv_item_name);
                tvItemValue = itemView.findViewById(R.id.tv_item_value);
            }
        }
    }
}
